package it.valeandre.omino;

import it.valeandre.omino.utils.Maps;

import java.util.Calendar;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class GCMMessageReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		String action = intent.getAction();
		if ("com.google.android.c2dm.intent.RECEIVE".equals(action)) {
//
			final String hometeam = intent.getStringExtra("hometeam");
			final String awayteam = intent.getStringExtra("awayteam");
			final String homegoal = intent.getStringExtra("homegoal");
			final String awaygoal = intent.getStringExtra("awaygoal");

			final String team = intent.getStringExtra("teamGoal");
			final String details = intent.getStringExtra("goalDetails");
			
			createNotification(context, hometeam, awayteam, homegoal, awaygoal, details, team);
		}
	}
	
//	public void createNotification(Context context) {
//		NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
//		Notification notification = createUpdateNotification();
//		// Hide the notification after its selected
//		notification.flags |= Notification.FLAG_AUTO_CANCEL;
//		Intent intent = new Intent(context, UpdateRequiredActivity.class);
//		intent.putExtra("update", "update");
//		
//		PendingIntent pendingIntent = PendingIntent.getActivity(context, Calendar.getInstance().get(Calendar.MILLISECOND), intent,
//				PendingIntent.FLAG_UPDATE_CURRENT);
//		
//		notification.defaults |= Notification.DEFAULT_SOUND;
//		 
//        notification.defaults |= Notification.DEFAULT_VIBRATE;
//		
//		notification.setLatestEventInfo(context, "Aggiornamento necessario!", "E' necessario aggiornare l'applicazione", pendingIntent);
//		notificationManager.notify(Calendar.getInstance().get(Calendar.MILLISECOND), notification);
//		
//	}

	public void createNotification(Context context, String hometeam, String awayteam, String homegoal, String awaygoal, String details, String team) {
		NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
		Notification notification = createNotificationWithIcon(team);
		// Hide the notification after its selected
		notification.flags |= Notification.FLAG_AUTO_CANCEL;

		Intent intent = new Intent(context, EventReceivedActivity.class);
		intent.putExtra("hometeam", hometeam);
		intent.putExtra("awayteam", awayteam);
		intent.putExtra("homegoal", homegoal);
		intent.putExtra("awaygoal", awaygoal);
		intent.putExtra("goalDetails", details);
		intent.putExtra("teamGoal", team);

		PendingIntent pendingIntent = PendingIntent.getActivity(context, Calendar.getInstance().get(Calendar.MILLISECOND), intent,
				PendingIntent.FLAG_UPDATE_CURRENT);

		String info = hometeam + " - " + awayteam + "   " + homegoal + " - " + awaygoal;

        notification.defaults |= Notification.DEFAULT_SOUND;

        notification.defaults |= Notification.DEFAULT_VIBRATE;

        String notificationTitle;
        if(team != null && !team.trim().equals("")) {
        	if(homegoal.equals("0") && awaygoal.equals("0")) {
        		notificationTitle = context.getResources().getString(R.string.match_beginning_notification_title).replace("{team1}", hometeam).replace("{team2}", awayteam);
        	} else {
        		notificationTitle = "Goal, " + team;
        	}
            
            int id = (hometeam + awayteam).hashCode();
            notification.setLatestEventInfo(context, notificationTitle, info, pendingIntent);
            notificationManager.notify(id, notification);
        }

	}
	
	@SuppressWarnings("deprecation")
	public Notification createUpdateNotification() {
		Notification notification = new Notification(R.drawable.palla_calcio, "Aggiornamento necessario!", System.currentTimeMillis());
		return notification;
	}

	@SuppressWarnings("deprecation")
	public Notification createNotificationWithIcon(String team) {

		Notification notification = new Notification(R.drawable.palla_calcio, "Evento partita", System.currentTimeMillis());

		for(String keyTeam: Maps.TEAM_LOGO_SMALL_MAP.keySet()) {
			try {
				if(team != null && team.contains(keyTeam)) {
					notification = new Notification(Maps.TEAM_LOGO_SMALL_MAP.get(keyTeam), "Ha segnato: " + keyTeam, System.currentTimeMillis());
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		return notification;
	}

}